package com.ermace.robo.jumper.game.GameObjects;

import java.util.HashMap;
import java.util.Map;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.ermace.robo.jumper.LoadingScreen;
import com.ermace.robo.jumper.R;

public class Bitmaps {
	
	private static Bitmaps instance = null;
	private static boolean init = false;
	
	private Map<Integer, Bitmap> bMap;
	private Resources resources;

	private Bitmaps(Resources res, LoadingScreen screen)
	{
		if (! init) {
			bMap = new HashMap<Integer, Bitmap >();
			resources = res;
			initBitmaps(screen);
		}
	}
	
	private Bitmaps(Resources res)
	{
		this(res, null);
	}
	
	public static synchronized Bitmaps getInstance(Resources res)
	{
		if(instance == null)
			instance = new Bitmaps(res);
		
		return instance;
	}
	
	/**
	 * 
	 * @return	The instance if it is initiated else null.
	 */
	public static synchronized Bitmaps getInstance()
	{
		return instance;
	}
	
	public static synchronized void loadAllBitmaps(Resources res, LoadingScreen screen) {
		instance = new Bitmaps(res, screen);
	}
	
	private void initBitmaps(LoadingScreen screen)
	{
		if (!init) {
			/*final R.drawable drawableResources = new R.drawable();
			final Class<R.drawable> c = R.drawable.class;
			final Field[] fields = c.getDeclaredFields();
	
			for (int i = 0, max = fields.length; i < max; i++) {
			    final int resourceId;
			    try {
			        resourceId = fields[i].getInt(drawableResources);
			    } catch (Exception e) {
			        continue;
			    }
			    Bitmap newB = BitmapFactory.decodeResource(resources, resourceId);
			    bMap.put(resourceId, newB);
			    if (screen != null)
			    	screen.incrementBitMapsLoaded();
			}
			
			init = true;*/
		    bMap.put(R.drawable.arrow_up_icon, BitmapFactory.decodeResource(resources, R.drawable.arrow_up_icon));
		    bMap.put(R.drawable.spritesheet, BitmapFactory.decodeResource(resources, R.drawable.spritesheet));
		    bMap.put(R.drawable.map_image_border, BitmapFactory.decodeResource(resources, R.drawable.map_image_border));
		    bMap.put(R.drawable.spritesheet_mapselection, BitmapFactory.decodeResource(resources, R.drawable.spritesheet_mapselection));
		    bMap.put(R.drawable.arrow_left, BitmapFactory.decodeResource(resources, R.drawable.arrow_left));
		    bMap.put(R.drawable.arrow_right, BitmapFactory.decodeResource(resources, R.drawable.arrow_right));
		    bMap.put(R.drawable.background_menu, BitmapFactory.decodeResource(resources, R.drawable.background_menu));
		}
	}
	
	public Bitmap getBitmap(int id)
	{
		return bMap.get(id);
	}
	

}
